class RemindPasswordController < ApplicationController
  skip_before_filter :sso, :prepare_session, :check_guest_mode
  verify :method => :post, :only => [:apply, :reset], :redirect_to => { :controller => :platform, :action => :index}
  layout 'accounts'

  def index
  end

  def apply
    # POST
    if account = Account.find_by_email(params[:email])
      apply_reset_account = ApplyResetAccount.create(:email => params[:email])
      UserMailer.deliver_sent_remind_password(apply_reset_account.email, url_for(:controller => :remind_password, :action => :confirm, :id => apply_reset_account.onetime_code))
      redirect_to :action => :sent_mail
    else
      flash[:notice] = "そのメールアドレスは登録されていません"
      render :action => :index
    end
  end

  def sent_mail
  end

  def confirm
    if apply_reset_account = ApplyResetAccount.find(:first,:conditions => ['onetime_code = ? and created_at > ?', params[:id], (Time.now - 1.hour)])
      @onetime_code = apply_reset_account.onetime_code
      @account = Account.new
    else
      render :action => :failure
    end
  end

  def reset
    unless apply_reset_account = ApplyResetAccount.find_by_onetime_code(params[:onetime_code])
      render :action => :failure
      return
    end
    @account = Account.find_by_email(apply_reset_account.email)
    @account.password = params[:account][:password]
    @account.password_confirmation = params[:account][:password_confirmation]
    if @account.save
      apply_reset_account.destroy
      redirect_to :action => :complete
    else
      @onetime_code = apply_reset_account.onetime_code
      render :action => :confirm
    end
  end

  def complete
  end
end
